package com.vecspace.data.model;

import com.vecspace.constant.AuthTag;
import com.vecspace.exception.HException;

/**数据的锁定状态
 * 用下划线命名变量，避免和POJO里定义的变量名冲突
 * @author wanghua
 * @date 2021/11/10 13:18
 * @copyright
 */
public class DataLockState {
    /**未锁定*/
    public final static Integer UNLOCKED = 0;
    /**已经锁定*/
    public final static Integer LOCKED = 1;
    /**已经存档，任何人不得修改*/
    public final static Integer ARCHIVED = 5;

    /** lockable默认为true
     *
     * @author wanghua
     * @date 2021/11/17  12:00
     **/
    public DataLockState(){
        this.lockable = true;
    }

    /**lockable默认为true
     *
     * @param lockState 锁定状态， 0未锁定，1已锁定，  5已归档，一般从记录中的ROW_LOCK字段获取
     * @author wanghua
     * @date 2021/11/17  12:06
     **/
    public DataLockState(Integer lockState){
        this.lockable = true;
        this.setLockState(lockState);
    }

    public DataLockState(boolean lockable){
        this.lockable = lockable;
    }

    /**是否使用数据锁,默认为true*/
    boolean lockable = true;
    /**锁定状态， 0未锁定，1锁定，  5归档*/
    int lockState = 0;


    /**是否使用数据锁,默认为true*/
    public boolean isLockable() {
        return lockable;
    }

    /**是否使用数据锁,默认为true*/
    public void setLockable(boolean lockable) {
        this.lockable = lockable;
    }

    /**是否锁定状态，包括归档状态也属于锁定状态*/
    public boolean isLocked() {
        return lockState > 0;
    }

    /**归档状态状态*/
    public boolean isArchived() {
        return lockState == ARCHIVED;
    }

    /**
     * @param lockState 锁定状态 0未锁定，1锁定，  5归档
     * @author wanghua
     * @date 2021/11/10  13:45
     * @return  void
     **/
    public void setLockState(Integer lockState){
        if(lockState == null) this.lockState = 0;
        else this.lockState = lockState;
    }

    /**锁定状态 0未锁定，1锁定，  5归档
     * @author wanghua
     * @date 2021/11/10  13:45
     * @return  void
     **/
    public int getLockState(){
        return this.lockState;
    }

    /** 判断锁定状态值是否在值域范围内
     * @param state
     * @author wanghua
     * @date 2021/11/17  16:47
     * @return boolean
     **/
    public static boolean isValidState(Integer state){
        return DataLockState.UNLOCKED.equals(state)
                || DataLockState.LOCKED.equals(state)
                || DataLockState.ARCHIVED.equals(state);
    }

    /**检查修改锁定状态的权限，不满足条件时抛出异常
     *
     * @param lockState
     * @param authTag
     * @author wanghua
     * @date 2021/11/17  20:09
     * @return void
     **/
    public static void checkAuth(Integer lockState, Integer authTag) throws Exception{
        if(AuthTag.None.equals(authTag) || AuthTag.Read.equals(authTag)){
            throw new HException("没有修改锁定状态的权限");
        }
        if(AuthTag.Write.equals(authTag) && DataLockState.ARCHIVED.equals(lockState)){
            throw new HException("没有将数据设置为归档状态的权限");
        }
    }
    
    /**判断是否锁定（以及归档）状态
     * 
     * @param lockState
     * @return
     */
    public static boolean isLockOrArchived(Integer lockState) {
    	if(lockState == null)return false;
    	if(DataLockState.LOCKED.equals(lockState))return true;
    	if(DataLockState.ARCHIVED.equals(lockState))return true;
    	return false;
    }
}
